package com.lh.java.connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OriginalConThread extends Thread {
	// 驱动程序名

	static String driver = "com.mysql.jdbc.Driver";

	// URL指向要访问的数据库名myad

	static String url = "jdbc:mysql://127.0.0.1:3306/myad";

	// MySQL配置时的用户名

	static String user = "root";

	// Java连接MySQL配置时的密码

	static String password = "root";
	
	//联结
	static Connection conn = null;
	
	// statement用来执行SQL语句
	static Statement statement  = null;
	
	static {
		// 加载驱动程序
		try {
			Class.forName(driver);
			// 连续数据库
			conn = DriverManager.getConnection(url, user, password);
			if (!conn.isClosed()){
				System.out.println("Succeeded connecting to the Database!");
			}
			statement = conn.createStatement();
		} catch (ClassNotFoundException e) {
			System.out.println("加载驱动程序出错");
		} catch (SQLException e) {
			System.out.println("联结出错");
		}
	
	}
	private String threadName;
	public OriginalConThread(String threadName){
		this.threadName  = threadName;
	}
	
	@Override
	public void run() {
//			try {
//				statement = conn.createStatement();
//			} catch (SQLException e1) {
//				System.out.println(Thread.currentThread().getName()+"\t"+"创建Statment失败"+"threadName="+threadName);;
//			}
		// 要执行的SQL语句
		String sql1 = "select * from class_student_id";
		ResultSet rs =null;
		try {
			rs = statement.executeQuery(sql1);
			System.out.println(" 学号" + "\t" + " 姓名");
			while (rs.next()) {				
				System.out.println(Thread.currentThread().getName()+"\t"+rs.getString("student_id") + "\t" + rs.getString("name")+"\t"+"threadName="+threadName);
				try {
				//	System.out.println(Thread.currentThread().getName()+"\t"+"休眠0.5秒"+"\t"+"threadName="+threadName);
					
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					//System.out.println(Thread.currentThread().getName()+"\t"+"休眠被打断"+"\t"+"threadName="+threadName);
				}
			}
			String sq2 = "select * from class_student_id";
			//rs.close();
			System.out.println("come to other");
			rs = statement.executeQuery(sq2);
			System.out.println("======= 年龄 ===========");
			while (rs.next()) {
				System.out.println(Thread.currentThread().getName()+"\t"+ rs.getInt("age")+"\t"+"threadName="+threadName);
				try {
					//System.out.println(Thread.currentThread().getName()+"\t"+"休眠0.5秒"+"\t"+"threadName="+threadName);
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					//System.out.println(Thread.currentThread().getName()+"\t"+"休眠被打断"+"\t"+"threadName="+threadName);
				}
			}
			System.out.println(Thread.currentThread().getName()+"\t"+"threadName="+threadName+"  is run over");
		} catch (SQLException e) {
			System.out.println("查找出错"+Thread.currentThread().getName()+"\t");
		}	
		finally{
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("关闭rs出错"+Thread.currentThread().getName()+"\t");
				}
			}
//			if(statement!=null){
//				try {
//					statement.close();
//				} catch (SQLException e) {
//					System.out.println("关闭statement出错"+Thread.currentThread().getName()+"\t");
//				}
//			}
//			if(conn!=null){
//				try {
//					conn.close();
//				} catch (SQLException e) {
//					System.out.println("关闭出错"+Thread.currentThread().getName()+"\t");
//				}
//			}
			
			
		}
	}

}
